package com.echo.yixiaowu.main.ui.search

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.VerticalDivider
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.echo.yixiaowu.R

@Composable
@Preview
fun SearchBar(modifier: Modifier = Modifier) {
    var text by remember { mutableStateOf("") }

    BasicTextField(value = text,
        onValueChange = { text = it },
        modifier = modifier
            .height(30.dp)
            .clip(RoundedCornerShape(8.dp))
            .background(Color.Black.copy(alpha = 0.1f)),
        singleLine = true,
        textStyle = TextStyle(color = Color.White, fontSize = 16.sp),
        decorationBox = { _ ->
            Row(
                modifier = Modifier.padding(horizontal = 12.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Icon(
                    painter = painterResource(R.drawable.ic_search),
                    contentDescription = "Search Icon",
                    tint = Color.White,
                    modifier = Modifier.size(18.dp)
                )

                // 输入框区域
                Box(
                    modifier = Modifier
                        .padding(start = 8.dp)
                        .weight(1f)
                ) {
                    if (text.isEmpty()) {
                        Text("搜索", color = Color.Gray, fontSize = 16.sp)
                    }
                }

                VerticalDivider(
                    modifier = Modifier
                        .height(21.dp)
                        .width(1.dp), color = Color.White
                )

                Spacer(Modifier.width(12.dp))

                Text("搜索", color = Color.White, fontSize = 16.sp, fontWeight = FontWeight.Bold)
            }
        })
}